package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.constant.MqConst;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.atguigu.gmall.service.RabbitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 支付表
 */
@Service
public class PaymentServiceImpl implements PaymentService {


    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;


    //先查询支付表是否存在了 如果不存在 保存支付表（部分信息）     如果存在了 返回
    @Override
    public PaymentInfo getPaymentInfo(Long orderId, PaymentType paymentType) {
        ////幂等性问题
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne
                (new QueryWrapper<PaymentInfo>().eq("order_id", orderId));
        if(null == paymentInfo){
            paymentInfo = new PaymentInfo();
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
            //对外业务编号
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderId);
            paymentInfo.setPaymentType(paymentType.name());
            //银行流水  后期再说了
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setCreateTime(new Date());
            //回调  回调时间
            paymentInfoMapper.insert(paymentInfo);
        }else{
            paymentInfo.setCreateTime(new Date());
            paymentInfo.setPaymentType(paymentType.name());
            paymentInfoMapper.updateById(paymentInfo);
        }
        return paymentInfo;
    }

    //回调之后更新支付表  幂等性问题？
    @Override
    public void updatePayment(Map<String, String> paramsMap) {
        //1:先查询
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>().
                eq("out_trade_no", paramsMap.get("out_trade_no")));
        //2:要不要更新
        if(paymentInfo.getPaymentStatus().equals(PaymentStatus.UNPAID.name())){
            //没有更新过 更新
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //更新流水号  / 支付状态 、回调时间  、回调内容
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            paymentInfo.setCallbackTime(new Date(paramsMap.get("notify_time")));
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            paymentInfoMapper.updateById(paymentInfo);
            //发消息  更新订单状态
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paramsMap.get("out_trade_no"));
        }
    }
    @Autowired
    private RabbitService rabbitService;
}
